package com.gexiaobao.pigeon.ui.fragment.mine.message

import android.graphics.Color
import android.os.Bundle
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.model.bean.MessageListResponse
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentMineMessageListBinding
import com.gexiaobao.pigeon.ui.activity.ActivityGameResult
import com.gexiaobao.pigeon.ui.adapter.MessageAllListAdapter
import com.gexiaobao.pigeon.ui.home.HomeViewModel
import com.kingja.loadsir.core.LoadService
import com.yanzhenjie.recyclerview.OnItemMenuClickListener
import com.yanzhenjie.recyclerview.SwipeMenuCreator
import com.yanzhenjie.recyclerview.SwipeMenuItem
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2023/5/31 18:51
 * @Describe    : 全部
 */
class MineMessageListFragment : BaseFragment<HomeViewModel, FragmentMineMessageListBinding>() {

    private val mAdapter: MessageAllListAdapter by lazy { MessageAllListAdapter(arrayListOf()) }
    private var messageData: ArrayList<MessageListResponse.DataList> = arrayListOf()
    private var title = ""
    private var raceId = 0

    //界面状态管理者
    private lateinit var loadsir: LoadService<Any>

    override fun initView(savedInstanceState: Bundle?) {

        val swipeMenuCreator = SwipeMenuCreator { _, rightMenu, _ ->
            val width = resources.getDimensionPixelSize(R.dimen.dp_70)
            val height = ViewGroup.LayoutParams.MATCH_PARENT
            val deleteItem = SwipeMenuItem(activity)
                .setBackground(R.drawable.round_b_g_5_train_fly_list_delete_red)
                .setTextColor(Color.WHITE)
                .setText(R.string.delete)
                .setTextSize(15)
                .setWidth(width)
                .setHeight(height)
            rightMenu.addMenuItem(deleteItem)
        }
        mDatabind.layoutList.swipeRecyclerView.setSwipeMenuCreator(swipeMenuCreator)

        val mItemMenuClickListener = OnItemMenuClickListener { menuBridge, position ->
            /** 任何操作必须先关闭菜单，否则可能出现Item菜单打开状态错乱 */
            menuBridge.closeMenu()
            /** 左侧还是右侧菜单 */
//            val direction = menuBridge.direction
//            /**  菜单在Item中的Position */
//            val menuPosition = menuBridge.position
            /** 调用删除接口 */
            showLoading(getString(R.string.loading))
            mViewModel.deleteMessage(mAdapter.data[position].id.toString())
        }
        mDatabind.layoutList.swipeRecyclerView.setOnItemMenuClickListener(mItemMenuClickListener)

        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, Util.dp2px(context, 5f)))
            it.initFooter {
                //触发加载更多时请求数据
                mViewModel.getMessageList(false, "")
            }
        }
        //状态页配置
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            //点击重试时触发的操作
            loadsir.showLoading()
            mViewModel.getMessageList(true, "")

        }

        mDatabind.layoutList.swipeRefreshLayout.init {
            loadsir.showLoading()
            mViewModel.getMessageList(true, "")

        }

        mAdapter.setOnItemClickListener { _, _, position ->
            if (mAdapter.data[position].messageType == 3) {
                if (mAdapter.data[position].passId == 0) {
                    RxToast.showToast("暂无跳转信息")
                    return@setOnItemClickListener
                }
                raceId = mAdapter.data[position].passId
                title = mAdapter.data[position].topic
                if (mAdapter.data[position].passStatus == 4 || mAdapter.data[position].passStatus == 5) {
                    mViewModel.getMemberNum(mAdapter.data[position].passId.toString())
                } else {
                    startActivity<ActivityGameResult>(
                        "title" to title,
                        "passId" to raceId,
                        "type" to "race"
                    )
                    activity?.overridePendingTransition(
                        android.R.anim.fade_in,
                        android.R.anim.fade_out
                    )
                }
            } else {
                nav().navigateAction(R.id.action_to_notice_detail, Bundle().apply {
                    putInt("id", mAdapter.data[position].id)
                })
            }
        }
    }

    override fun onResume() {
        super.onResume()
        loadsir.showLoading()
        mViewModel.getMessageList(true, "")
    }

    override fun createObserver() {
        super.createObserver()

        mViewModel.memberNumResult.observe(viewLifecycleOwner) {
            if (it != null) {
                nav().navigateAction(R.id.action_to_sign_up_detail, Bundle().apply {
                    putString("title", title)
                    putString("memberCode", it.memberNum)
                    putInt("id", raceId)
                })
            }
        }

        mViewModel.messageList.observe(viewLifecycleOwner) {
            if (it.listData?.isEmpty() == false) {
                messageData = it.listData
            }
            loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
        }

        mViewModel.deleteMsgResult.observe(viewLifecycleOwner) {
            dismissLoading()
            if (it.isSuccess) {
                loadsir.showLoading()
                mViewModel.getMessageList(true, "")
            } else {
                RxToast.showToast(it.errorMsg)
            }
        }
    }

    companion object {
        fun newInstance(): MineMessageListFragment {
            return MineMessageListFragment()
        }
    }
}